package com.uzai.console.dto;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.CommonConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 查询参数
 */
@ApiModel("分页参数")
public class QueryParamApi implements Serializable  {
    private static final long serialVersionUID = -8760973809589144543L;
    @ApiModelProperty("当前页")
	private Integer current = CommonConstant.DEFAULT_PAGE_INDEX;
    @ApiModelProperty("页大小")
	private Integer size = CommonConstant.DEFAULT_PAGE_SIZE;
    @ApiModelProperty("排序字段")
	private String orderByField;
    @ApiModelProperty("是否升序")
	private boolean isAsc;
    @ApiModelProperty("关键字搜索")
    private String keyword;
    @ApiModelProperty("排序数组")
    private List<OrderItem> orderItemList;
    @ApiModelProperty(value = "开始下标",hidden = true)
    private Integer fromIndex;
    @ApiModelProperty(value = "id开始值", hidden = true)
    private Long idStart;
    @ApiModelProperty(value = "升序还是降序", hidden = true)
    private String order;
    @ApiModelProperty("是否查询所有数据(0-分页查询，1-查询所有)")
    private int selectAll;

	public QueryParamApi() {

    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
	    if (current == null || current == 0){
	        this.current = CommonConstant.DEFAULT_PAGE_INDEX;
        }else{
            this.current = current;
        }
    }

    public Integer getSize() {
	    if(size == null || size == 0){
            size = CommonConstant.DEFAULT_PAGE_SIZE;
        }
	    if(size > 100){
            size = 100;
        }
        return size;
    }

    public void setSize(Integer size) {
	    if (size == null || size == 0){
	        this.size = CommonConstant.DEFAULT_PAGE_SIZE;
        }else{

            if(size > 100){
                size = 100;
            }

            this.size = size;
        }
    }

    public String getOrderByField() {
	    //驼峰转下划线
	    if(StringUtils.isNotBlank(orderByField)){
            orderByField = Tools.humpToLine(orderByField);
        }else{
            orderByField = "id";
        }
        return orderByField;
    }

    public void setOrderByField(String orderByField) {
        this.orderByField = orderByField;
    }

    public boolean isAsc() {
        return isAsc;
    }

    public void setAsc(boolean asc) {
        isAsc = asc;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public void convert(Page page){
        page.setCurrent(this.getCurrent());
        page.setSize(this.getSize());

        //排序数组不为空，则优先排序
        if(orderItemList != null && orderItemList.size() > 0){
            for (OrderItem orderItem : orderItemList){
                if(StringUtils.isNotBlank(orderItem.getColumn())){
                    //转成按照中文gbk来排序
                    page.addOrder(orderItem.setColumn(Tools.humpToLine(orderItem.getColumn())));
                }
            }
        }else{
            if (this.getOrderByField() !=null ){
                if (this.isAsc()){
                    page.addOrder(OrderItem.asc(this.getOrderByField()));
                }else{
                    page.addOrder(OrderItem.desc(this.getOrderByField()));
                }
            }else{
                page.addOrder(OrderItem.desc("id"));
            }
        }
    }

    public List<OrderItem> getOrderItemList() {
        return orderItemList;
    }

    public void setOrderItemList(List<OrderItem> orderItemList) {
        this.orderItemList = orderItemList;
    }

    @Override
    public String toString() {
        return "QueryParam{" +
                "current=" + current +
                ", size=" + size +
                ", orderByField='" + orderByField + '\'' +
                ", isAsc=" + isAsc +
                ", keyword='" + keyword + '\'' +
                '}';
    }

    public Integer getFromIndex() {
	    fromIndex = (this.getCurrent() - 1) * this.getSize();
        return fromIndex;
    }

    public void setFromIndex(Integer fromIndex) {
        this.fromIndex = fromIndex;
    }

    public Long getIdStart() {
        return idStart;
    }

    public void setIdStart(Long idStart) {
        this.idStart = idStart;
    }

    public String getOrder() {
	    if(StringUtils.isBlank(order)){
            if(this.isAsc){
                order = "asc";
            }else{
                order = "desc";
            }
        }
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public int getSelectAll() {
        return selectAll;
    }

    public void setSelectAll(int selectAll) {
        this.selectAll = selectAll;
    }
}
